import pypandoc
import os

def convert_markdown_to_pdf(md_file_path, output_pdf_path):
    # 确保输出目录存在
    output_dir = os.path.dirname(output_pdf_path)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    print(f"正在将 '{md_file_path}' 转换为 '{output_pdf_path}'...")
    try:
        # 转换时需要指定xelatex引擎以支持中文
        extra_args = ['--pdf-engine=xelatex', '-V', 'mainfont=SimSun']
        output = pypandoc.convert_file(md_file_path, 'pdf', outputfile=output_pdf_path, extra_args=extra_args)
        assert output == ""
        print("转换成功！")
    except Exception as e:
        print(f"转换失败: {e}")

if __name__ == '__main__':      
    convert_markdown_to_pdf("test.md", "out/report.pdf")